package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerService;

public class CustomerServiceImpl implements CustomerService{

	private static List<Customer> list = new ArrayList<Customer>(); 
	
	@Override
	public synchronized Customer addCustomer(Customer customer) {
		String id = UUID.randomUUID().toString();
		customer.setId(id);
		list.add(customer);
		return customer;
	}

	@Override
	public synchronized boolean deleteCustomer(String id) {
		for (int i = 0; i < list.size(); i++) {
			Customer customer = list.get(i);
			if(customer.getId().equals(id)){
				list.remove(i);
				return true;
			}
		}
		return false;
	}

	@Override
	public synchronized boolean updateCustomer(Customer customer) {
		String id = customer.getId();
		for (int i = 0; i < list.size(); i++) {
			if(id.equals(list.get(i).getId())){
				list.set(i, customer);
				return true;
			}
		}
		return false;
	}

	@Override
	public List<Customer> findAll() {

		return list;
	}
	
	public static void main(String[] args) {
		Customer c = new Customer("1", "mike", false, 12, new Date());
		Customer cust = new Customer("1", "matthew", true, 10000, new Date());
		CustomerServiceImpl cs = new CustomerServiceImpl();
		cs.addCustomer(c);
		cust.setId(c.getId());
		cs.updateCustomer(cust);
		System.out.println(cs.findAll());
	}
}
